package com.abe.jmcl.android.retrofit.contract;


import android.util.Base64;

import com.abe.jmcl.api.AppMainService;
import com.abe.jmcl.entity.http.test.HttpBaseParamModel;
import com.abe.jmcl.entity.http.test.HttpUserInfoEntity;
import com.abe.jmcl.entity.http.test.HttpBaseReturnModel;
import com.abe.jmcl.entity.http.test.param.ParamLogin;
import com.abeframe.core.base.http.ResultListener;
import com.abeframe.core.utils.Base64Utils;
import com.google.gson.Gson;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CommonModel implements CommonContract.Model {
    @Override
    public void getData(final ResultListener<HttpBaseReturnModel<HttpUserInfoEntity>> listener) {
        ParamLogin paramLogin = new ParamLogin("system", "4a7d1ed414474e4033ac29ccb8653d9b");
        HttpBaseParamModel<ParamLogin> model=new HttpBaseParamModel<>();
        model.data=paramLogin;
        String strEntity = new Gson().toJson(model);
        String base64Params= Base64Utils.getBase64(strEntity);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), base64Params);
        Call<HttpBaseReturnModel<HttpUserInfoEntity>> call = AppMainService.getWeManagerService().checkLogin(body);
        call.enqueue(new Callback<HttpBaseReturnModel<HttpUserInfoEntity>>() {
            @Override
            public void onResponse(Call<HttpBaseReturnModel<HttpUserInfoEntity>> call, Response<HttpBaseReturnModel<HttpUserInfoEntity>> response) {
                listener.onSuccess(response.body());
                listener.onEnd();
            }

            @Override
            public void onFailure(Call<HttpBaseReturnModel<HttpUserInfoEntity>> call, Throwable t) {
                listener.onFailure(t.getMessage());
                listener.onEnd();
            }
        });
    }
}
